<template>
  <el-dialog
    v-model="model"
    title="添加库存信息"
    width="800px"
    :close-on-click-modal="false"
    :before-close="handleClose"
  >
    <div class="cntr">
      <el-form
        ref="form"
        :model="editFormFields"
        :rules="rules"
        label-width="120px"
      >
        <el-row>
          <el-col :span="12">
            <el-form-item label="库存数量" prop="RawmanagementKCNum">
              <el-input-number
                v-model="editFormFields.RawmanagementKCNum"
                :min="0"
                controls-position="right"
              ></el-input-number>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="库存上限" prop="RawmanagementSx">
              <el-input-number
                v-model="editFormFields.RawmanagementSx"
                :min="0"
                controls-position="right"
              ></el-input-number>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="库存下限" prop="RawmanagementXx">
              <el-input-number
                v-model="editFormFields.RawmanagementXx"
                :min="0"
                controls-position="right"
              ></el-input-number>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="存放位置" prop="RawmanagementWZ">
              <el-input v-model="editFormFields.RawmanagementWZ"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </div>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="handleClose">取 消</el-button>
        <el-button type="primary" @click="save">确 定</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script>
export default {
  data() {
    return {
      model: false,
      editFormFields: {
        RawmanagementKCNum: 0,
        RawmanagementSx: 0,
        RawmanagementXx: 0,
        RawmanagementWZ: "",
      },
      rules: {
        RawmanagementKCNum: [
          { required: true, message: "请输入库存数量", trigger: "blur" },
        ],
        RawmanagementSx: [
          { required: true, message: "请输入库存上限", trigger: "blur" },
        ],
        RawmanagementXx: [
          { required: true, message: "请输入库存下限", trigger: "blur" },
        ],
        RawmanagementWZ: [
          { required: true, message: "请输入存放位置", trigger: "blur" },
        ],
      },
    };
  },
  methods: {
    open() {
      this.model = true;
    },
    handleClose() {
      this.model = false;
      this.$refs.form.resetFields();
    },
    save() {
      this.$refs.form.validate((valid) => {
        if (valid) {
          let $parent = null;
          this.$parent.$emit("parentCall", ($this) => {
            $parent = $this;
          });
          
          if (!$parent || !$parent.currentRow || !$parent.currentRow.RawmanagementId) {
            return this.$Message.error("请先选择原料信息");
          }

          const data = {
            ...this.editFormFields,
            RawmanagementId: $parent.currentRow.RawmanagementId
          };

          this.http
            .post("api/Raw/addStock", data)
            .then((res) => {
              if (res.success) {
                this.$Message.success("保存成功");
                this.$parent.setTable2Rows([res.data]);
                this.handleClose();
              } else {
                this.$Message.error(res.message);
              }
            });
        }
      });
    },
  },
};
</script>

<style lang="less" scoped>
.cntr {
  padding: 15px 20px;
}
.dialog-footer {
  text-align: right;
  padding-right: 20px;
}
</style> 